﻿using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Infrastructure.Dto;
using BenCaoXuan.Infrastructure.Dto.Admin.employee;
using BenCaoXuan.Infrastructure.EFCore.MySql;
using BenCaoXuan.Infrastructure.IOC;
using BenCaoXuan.Infrastructure.Redis;
using BenCaoXuan.Infrastructure.Tools;
using BenCaoXuan.Service.Interface.PC;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BenCaoXuan.Service.Instance.PC
{
    [Inject_]
    public class PcLogin_Service : Base_Service, I_PcLogin_Service
    {
        private readonly Login_IOC _login_IOC;
        private readonly IConfiguration _configuration;
        private readonly RedisStringService _redisStringService;

        public PcLogin_Service(Login_IOC login_IOC, IConfiguration configuration, RedisStringService redisStringService)
        {
            _login_IOC = login_IOC;
            _configuration=configuration;
            _redisStringService=redisStringService;
        }

        /// <summary>
        /// 检查登录
        /// </summary>
        /// <param name="code"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public Employee_Dto Check_Pc_Login(string? code, string? account)
        {

            var user = _redisStringService.Get<string>(code);//通过code 查看是否有保存用户的信息

            if (!string.IsNullOrEmpty(user))
            {
                //有就输出返回
                return JsonConvert.DeserializeObject<Employee_Dto>(user);
            }

            //没有就保存
            var key = _configuration["Redis:Keys:Check_PC_Login"];
            var user_code = _redisStringService.Get<string>(key + account);

            if (user_code == code)
            {
                var data = _login_IOC._sysEmployee_EFCore.GetAll(d => d.EAccount == account).Select(d => new Employee_Dto
                {
                    id = d.EId,
                    account = account,
                    name = d.EName,
                    photo=d.EPhoto
                }).SingleOrDefault();

                _redisStringService.Set<string>(code, JsonConvert.SerializeObject(data), DateTime.Now.AddHours(24));

                return data;
            }
            else
            {
                return null;
            }


        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Result_Dto> Logins(Login_Dto dto)
        {
            var iq = _login_IOC._sysEmployee_EFCore.GetAll(d => d.EAccount == dto.account && d.EDepartmentId == "35E57BF24FE54091BBD0E68BA3191C7F" && d.EPostId == "04091b06026d0002f34de68e7f180449");
            if (!await iq.AnyAsync())
            {
                return Get_Result(-1, "账号或者密码错误");
            }
            var user = await iq.SingleAsync();
            var pwd = EncryptUtil.LoginMd5(dto.password, user.ESalt);
            if (user.EPassword != pwd)
            {
                return Get_Result(-1, "账号或者密码错误");
            }
            if (user.EIsBan != 0)
            {
                return Get_Result(-1, "账号或者密码错误");
            }

            SysPcLoginLog sysPcloginLog = new SysPcLoginLog()
            {
                LlId = Config.GUID2(),
                LlEid = user.EId,
                LlCode = dto.code,
                LlEname = user.EName!,
                LlIpaddress = Config.GetIp(),
                LlLoginTime = DateTime.Now,
                LlRemark = user.ERemark,
            };

            //保存登录日志
            _login_IOC._sysPcLoginLog_EFCore.Add(sysPcloginLog);
            var result = await _login_IOC._sysPcLoginLog_EFCore.SaveChangesAsync();

            //保存code到redis
            var jwt = _configuration["Redis:Keys:Check_PC_Login"];

            _redisStringService.Set<String>(jwt + user.EAccount, dto.code!, DateTime.Now.AddSeconds(86400));

            return Get_Result(result == 1 ? 1 : -1);
        }


    }
}
